"use strict";

Components.utils.import("resource://gre/modules/DownloadUtils.jsm")

if ("undefined" == typeof(viewSoso)) {
    var viewSoso = { }
}

viewSoso.main = {
  _preference : "browser.cache.disk.enable",

//  set size of disk cache
  visitEntries :
  {    // from advanced.js of preferences
      visitDevice: function (deviceID, deviceInfo)
      {
         if (deviceID == "disk")
         try {
/*    dump("_dvk_dbg_, cache totalSize:\t"); dump(deviceInfo.totalSize); dump("\n\n");
    var sizeStrings = DownloadUtils.convertByteUnits(deviceInfo.totalSize);
    var prefStrBundle = document.getElementById("bundlePreferences");
    var sizeStr = prefStrBundle.getFormattedString(
        device == "disk" ? "actualDiskCacheSize" : "actualAppCacheSize", sizeStrings);  */

         document.getElementById("clearCacheButton").setAttribute("disabled", "false");

            if(deviceInfo.totalSize > (1024 * 1000))
            {
                let thesize = DownloadUtils.convertByteUnits(deviceInfo.totalSize);

                var thelement = document.getElementById("actualDiskCacheSize");
                thelement.value = moduleViewSoso.format.replace("{.}", thesize.join(" "));
                thelement.setAttribute("control", "clearCacheButton");
            }
            else
                viewSoso.main.oneCacheSize();
         }
         catch (e) {
            Components.utils.reportError(e)
         }
    //  ..., otherwise returns false to advance to the next device.
        return false; // Do not enumerate entries
      },

      visitEntry: function (deviceID, entryInfo)
      {
         return false;   // Do not enumerate entries.
      }
  },

  oneCacheSize : function()
  {
      var thenode = document.getElementById("actualDiskCacheSize");
         thenode.value = moduleViewSoso.onemb;
         thenode.removeAttribute("control");
  },
  
  resetDlg : function()
  {
//    dump("_dvk_dbg_, reset dialog.\n");
      var thebtn = document.getElementById("clearCacheButton");
         thebtn.setAttribute("disabled", "true");

      var thelement = document.getElementById("actualDiskCacheSize");
         thelement.value = thelement.parentNode.getAttribute("value");
         thelement.removeAttribute("control");
  },
  
  startup: function() // Initialize the extension
  {
      if(Services.prefs.getBoolPref(this._preference))
          Services.cache.visitEntries(this.visitEntries);
      Services.prefs.addObserver(this._preference, this, false);
  },

  observe: function(asubject, atopic, adata)
  {
      if(atopic === "nsPref:changed")
         if(adata == this._preference)
         try {
             if(Services.prefs.getBoolPref(this._preference))
                 window.setTimeout(Services.cache.visitEntries, 1, this.visitEntries)
             else this.resetDlg();
         }
         catch (err) {
             Components.utils.reportError(err)
         }
//	dump("_dvk_dbg_, subject:\t"); dump(asubject); dump("\n");
  },

  clearCache : function ()
  {
      try {
         Services.cache.evictEntries(
             Components.interfaces.nsICache.STORE_ON_DISK);
         this.oneCacheSize();
      }
      catch(err)
      {
         Components.utils.reportError(err)
      }
  },

  handleEvent: function( evt ) // load/unload the extension
  {
      window.removeEventListener(evt.type, this, false);

      if (evt.type == "load") this.startup()
         else
            Services.prefs.removeObserver(this._preference, this);
  }
}

